package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.constant.StatusEnum;
import com.fivefu.core.camera.entity.DbMonitorWorkorder;
import com.fivefu.core.camera.services.DbMonitorWorkorderService;
import com.fivefu.core.camera.services.SceneEventService;
import com.fivefu.core.camera.vo.request.EventExamineVo;
import com.fivefu.core.camera.vo.request.WorkorderVo;
import com.fivefu.core.camera.vo.response.EventCameraBean;
import com.fivefu.core.camera.vo.response.EventExamineBean;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 场景工单表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-19 19:38:06
 */
@Api(tags = "事件管理")
@RestController
@RequestMapping("/db-event")
public class DbEventController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(DbEventController.class);

    @Autowired
    private SceneEventService sceneEventService;
    @Autowired
    private DbMonitorWorkorderService dbMonitorWorkorderService;


    @RequestMapping(path = "/queryEventList",method = RequestMethod.POST)
    @ApiOperation(value = "查询事件管理列表",httpMethod = "POST",response = ResultInfo.class,notes = "查询事件管理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int"),

            @ApiImplicitParam(name = "sceneCode", value = "场景标识", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "areaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "sStime", value = "开始时间(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "sEtime", value = "开始时间(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "eStime", value = "结束时间(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "eEtime", value = "结束时间(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "queryType", value = "查询类型(1:待审核, 2:审核不合格)", paramType = "query",required = false, dataType = "int")
    })
    public ResultInfo queryEventList(Integer page, Integer limit,Integer queryType){
        try {
            logger.info("查询事件管理列表接收的参数："+ request.getParameterMap());
            if (page == null){
                ResultInfo.renderError("页码不能为空");
            }
            if (limit == null){
                ResultInfo.renderError("数量不能为空");
            }
            String sceneCode = StrUtils.isCheckNull(request.getParameter("sceneCode"));
            String areaid = StrUtils.isCheckNull(request.getParameter("areaid"));
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));
            String sStime = StrUtils.isCheckNull(request.getParameter("sStime"));
            String sEtime = StrUtils.isCheckNull(request.getParameter("sEtime"));
            String eStime = StrUtils.isCheckNull(request.getParameter("eStime"));
            String eEtime = StrUtils.isCheckNull(request.getParameter("eEtime"));

            //验证时间格式
            if (StrUtils.isNotNull(sStime)){
                try {
                    VerifyDataUtils.verifyDateTime(sStime, "yyyy-MM-dd HH:mm:ss");
                    VerifyDataUtils.verifyDateTime(sEtime, "yyyy-MM-dd HH:mm:ss");
                }catch (Exception e){
                    return ResultInfo.renderError("开始时间格式格式不正确");
                }
            }
            if (StrUtils.isNotNull(eStime)){
                try {
                    VerifyDataUtils.verifyDateTime(eStime, "yyyy-MM-dd HH:mm:ss");
                    VerifyDataUtils.verifyDateTime(eEtime, "yyyy-MM-dd HH:mm:ss");
                }catch (Exception e){
                    return ResultInfo.renderError("结束时间格式格式不正确");
                }
            }
            Page<EventCameraBean> list = sceneEventService.queryEventList(sceneCode, areaid, cameraName, onlineStatus, sStime, sEtime, eStime, eEtime, queryType, page, limit);
            return ResultInfo.renderSuccess(200,"查询事件管理列表成功",list.getTotal(),list.getRecords());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return   ResultInfo.renderError("查询事件管理列表失败");
    }

    @RequestMapping(path = "/queryEventDetail",method = RequestMethod.POST)
    @ApiOperation(value = "查询事件详情",httpMethod = "POST",response = ResultInfo.class,notes = "查询事件详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventId", value = "事件编号", paramType = "query",required = true, dataType = "Long"),
    })
    public ResultInfo queryEventDetail(Long eventId){
        try {
            logger.info("查询事件详情接收数据：" + JSON.toJSONString(request.getParameterMap()));
            if (eventId == null){
                return ResultInfo.renderError("事件编号不能为空");
            }
            Map<String, Object> detail = sceneEventService.queryEventDetail(eventId);

            return ResultInfo.renderSuccess(detail);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询事件详情失败");
    }

    @RequestMapping(path = "/createOrderTemplate",method = RequestMethod.POST)
    @ApiOperation(value = "拟生成工单",httpMethod = "POST",response = ResultInfo.class,notes = "拟生成工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventId", value = "事件编号", paramType = "query",required = true, dataType = "Long"),
    })
    public ResultInfo createOrderTemplate(Long eventId){
        try {
            logger.info("拟生成工单接收数据：" + JSON.toJSONString(request.getParameterMap()));
            if (eventId == null){
                return ResultInfo.renderError("事件编号不能为空");
            }
            EventExamineBean event = sceneEventService.createOrderTemplate(eventId);
            return ResultInfo.renderSuccess(event);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("拟生成工单失败");
    }

    @RequestMapping(path = "/examineEvent",method = RequestMethod.POST)
    @ApiOperation(value = "事件审核",httpMethod = "POST",response = ResultInfo.class,notes = "事件审核")
    public ResultInfo examineEvent(@ModelAttribute @Valid EventExamineVo examineVo, @ModelAttribute @Valid WorkorderVo workorderVo, @RequestAttribute String userid){
        try {
            logger.info("事件审核接收数据：" + JSON.toJSONString(request.getParameterMap()));

            String value = VerifyDataUtils.verifyRequiredColumn(examineVo);
            if (StrUtils.isNotNull(value)){
                return ResultInfo.renderError(value + "不能为空");
            }
            if (examineVo.getExamineStatus().equals(StatusEnum.EXAMINE_HEGE.getStatusId())){
            //if (examineVo.getExamineStatus().equals(StatusEnum.EXAMINE_HEGE.getStatusId())){
                value = VerifyDataUtils.verifyRequiredColumn(workorderVo);
                if (StrUtils.isNotNull(value)){
                    return ResultInfo.renderError(value + "不能为空");
                }
            }
            Long orderId = sceneEventService.examineEvent(examineVo, workorderVo, userid);
            if (orderId != null){
                DbMonitorWorkorder order = dbMonitorWorkorderService.queryWorkorderById(orderId);
                Map<String,Object> map = new HashMap<>();
                map.put("id", order.getId());
                map.put("orderId", order.getOrderId());

                return ResultInfo.renderSuccess(200,"审核成功，工单编号为：" + order.getOrderId(),map);
            }else{
                return ResultInfo.renderSuccess("审核成功");
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("审核失败");
    }

}
